package other;

import java.io.Serializable;
import java.lang.reflect.Constructor;

/**
 * create by Sun
 * 2018/8/8
 */
public class SingleDemo implements Serializable{

    private static boolean flag=true;
    private static SingleDemo single1;
    private SingleDemo(){
        if(single1==null){
            System.out.println("11私有構造方法");
        }
        /**
         * 防止使用反射来进行创建新的对象的
         */
        if (single1!=null){
            throw new RuntimeException();
        }
        if(flag==true){
            flag=!flag;
        }
        if(!flag){
            throw new RuntimeException("的説法的身份");
        }
    }

   public SingleDemo readResolve(){
        return single1;
   }

    //    public static class CreateSingleDemo{
//        //private static final SingleDemo single=new SingleDemo();
//        public static SingleDemo getInstance(){
//            single1=new SingleDemo();
//            return single1;
//        }
//    }
//    public static  SingleDemo getInstance(){
//        return CreateSingleDemo.getInstance();
//    }
    public static void main(String[] args) throws Exception {


        Class<SingleDemo> clazz= (Class<SingleDemo>) Class.forName("other.SingleDemo");
        Constructor<SingleDemo> declaredConstructor = clazz.getDeclaredConstructor(null);
        SingleDemo singleDemo = declaredConstructor.newInstance();
        System.out.println(singleDemo);
        SingleDemo singleDemo1 = declaredConstructor.newInstance();
        System.out.println(singleDemo1);
    }
}
